﻿/*
*
*
*   My MSDN Download Script
*
*
*/

$(function () {


    // function of getting family(ies)
    var getFamily = function (a, b, c) {

        var $family = $("#search_output");

        $.ajax({
            type: "GET",
            cache: false,
            url: "partial-family.html",
            beforeSend: function () {
                $family.html('<div class="loading"></div>');
            },
            success: function (result) {
                $family.fadeOut(function () {

                    $(this).html(result).fadeToggle();
                    initFamily(true, 0, 0);

                });
            },
            error: function (xhr, msg, e) {
                alert(msg);
            }
        });

    };

    // function of getting product(s)
    var getProduct = function ($dd, b, c, d) {

        $.ajax({
            type: "GET",
            cache: false,
            url: "partial-product.html",
            beforeSend: function () {
                $dd.html('<div class="loading"></div>').slideDown();
            },
            success: function (result) {

                $dd.fadeOut(function () {

                    $(this).
                        html(result).
                        fadeToggle().
                        children().
                        click(function () {
                            getShip(0, 0, 0, 0, 0);
                        });

                });

            },
            error: function (xhr, msg, e) {
                alert(msg);
            }
        });

    };

    // function of getting ship(s)
    var getShip = function (a, b, c, d, e) {

        var target = (Math.random() > 0.5 ? "partial-ship.html" : "partial-ship-empty.html");

        $.ajax({
            type: "GET",
            cache: false,
            url: target,
            beforeSend: function () {
                $("#mmcxt").html('<div class="boxLoading"></div>');
            },
            success: function (result) {
                $("#mmcxt").fadeOut(function () {

                    $(this).html(result).fadeToggle();


                    // Table隐藏
                    $("#tbody tr:odd").hide();
                    $(".det").click(function () {
                        var tr = $(this).parent().parent().next();
                        if (tr.is(":hidden"))
                            tr.show();
                        else
                            tr.hide();
                    });
                    // Table单双行变色
                    $("#tbody tr:even").each(function (index) {
                        if (index % 2 == 0) {
                            $(this).addClass('odd');
                        }
                    });
                    //table排版		
                    $('#tbody tr:odd div').addClass('detail_tr');
                    $('#tbody tr:even').each(function () { $(this).children('td').first().css('text-align', 'left').css('padding', '0 15px'); });

                    // header event
                    $("#thead a").click(function () {
                        getShip(0, 0, 0, 0, 0);
                    });

                });
            },
            error: function (xhr, msg, e) {
                alert(msg);
            }
        });

    };

    // function of initialize family event binding
    var initFamily = function (searchMode, b, c) {

        //右侧列表手风琴
        $('#search_output dt').each(function () {

            $(this).click(function () {

                $(this).toggleClass('click');

                if (!searchMode && !$(this).data('initialized')) {

                    var $dd = $(this).next();
                    getProduct($dd, 0, 0, 0);

                    $(this).data('initialized', true);

                } else {
                    $(this).next().slideToggle();
                }

            });

            if (searchMode && !$(this).data('initialized')) {

                $(this).
                        next().
                        children().
                        click(function () {
                            getShip(0, 0, 0, 0, 0);
                        });

                $(this).data('initialized', true);
            }

            $(this).hover(function () {
                $(this).toggleClass('hover');
            });

            if (!searchMode)
                $(this).next().hide();
            else
                $(this).toggleClass('click');

        });
    };

    // function of initialize product event binding
    var initProduct = function (a, b, c) {

    };

    // function of initialize search event binding
    var initSearch = function (a, b, c) {

        $("#search_box").each(function () {
            $(this).
            data("tips", $(this).val()).
            focus(function () {
                if ($(this).val() == $(this).data("tips"))
                    $(this).val("");
                $(this).addClass("focus");
            }).blur(function () {
                if ($(this).val() == "")
                    $(this).val($(this).data("tips"));
                $(this).removeClass("focus");
            }).bind("keydown", function (e) {
                if (e.which == 13)
                    getFamily(0, 0, 0);
            });
        });

        //SearchButton样式
        $('#search_btn').hover(function () {
            $(this).toggleClass('hover');
        }).click(function () {
            getFamily(0, 0, 0);
        });
    };

    // function of initialize language filter
    var initLanguage = function (a, b, c) {

        var $dl = $("#lan li dl");
        var locales = [
                        ["1033", "English", "flag-american"],
                        ["2052", "简体中文", "flag-china"],
                        ["0000", "繁體中文", "flag-china-tr"],
                        ["0000", "Français", "flag-france"],
                        ["0000", "Deutsch", "flag-germany"],
                        ["0000", "Italiano", "flag-italy"],
                        ["0000", "日本語", "flag-japan"],
                        ["0000", "한글", "flag-korea"],
                        ["0000", "Português", "flag-portugal"],
                        ["0000", "Русский язык", "flag-russian"],
                        ["0000", "Español", "flag-spain"]
                      ];


        for (var i in locales) {
            $dl.append("<dd title='" + locales[i][1] + "'><a><div class='" + locales[i][2] + "'></div><h2>" + locales[i][1] + "</h2></a></dd>").
                children().
                last().
                data("lcid", locales[i][0]).
                click(function () {
                    $('#lb_locale').html($(this).attr("title"));
                    getShip(0, 0, 0, 0, 0);
                    $dl.hide();
                });
        }


        $('#lan li a').click(function () {
            $dl.show();
        });
        $('#lan li dl dd').hover(function () {
            $(this).toggleClass('selected');
        });
        $('#lan_close').click(function () {
            $dl.hide();
        });
    };


    // initialize family and product if not in search mode
    initFamily(false, 0, 0);

    // initialize search pane
    initSearch(0, 0, 0);

    // initialize language pane
    initLanguage(0, 0, 0);

});
